﻿using LdtJudge.DataEngine.Data.DataParseStorage;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.OptimizationModel;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Ldt.WindowsFormsApp
{

    /// <summary>
    /// 显示案件数据
    /// </summary>
    public partial class FormShowCaseData : UIForm
    {
        /// <summary>
        /// 要查询的案件唯一ID
        /// </summary>
        private string caseOnlyID = "";
        /// <summary>
        /// 案件信息的全部数据
        /// </summary>
        private List<CaseCenterModel> caseModelDatas = null;


        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="_caseOnlyID"></param>
        public FormShowCaseData(string _caseOnlyID)
        {
            InitializeComponent();
            caseOnlyID = _caseOnlyID;
        }

        /// <summary>
        /// 查询到当前案件的全部数据
        /// </summary>
        /// <returns></returns>
        private void LoadData()
        {
            List<An_anjianxinxi> an_Anjianxinxis = new List<An_anjianxinxi>();
            DAL_An_anjianxinxi dAL_An_Anjianxinxi = new DAL_An_anjianxinxi();
            an_Anjianxinxis = dAL_An_Anjianxinxi.GetList(caseOnlyID);

            //List<CaseCenterModel> caseCenterList = new List<CaseCenterModel>();
            //DAL_CaseCenter dAL_Case = new DAL_CaseCenter("casecenter");
            //Dictionary<string, string> dicopwhere = new Dictionary<string, string>()
            //{
            //    {"casecenter", "  a.caseonlyid='" + caseOnlyID + "' " }
            //};
            ////dicopwhere.Add("casecenter", "  caseonlyid='" + caseCasusID + "' ");
            //caseModelDatas = dAL_Case.Query(dicopwhere);

        }

        private void LoadTreeView(List<CaseCenterModel> caseCenters)
        {
            TreeNode root = new TreeNode();
            root.Text = "民事案件信息要素中心";
            treeViewCaseStruct.Nodes.Add(root);

            if (caseCenters.Count > 0)
            {
                //获取到所有案件数据集合
                var elementDataList = caseCenters[0].ElementDataModelList;

                var rootNode = elementDataList.FirstOrDefault(n => n.XmlPath == "民事案件信息要素中心" && string.IsNullOrWhiteSpace(n.ParentID));
                if (rootNode != null)
                {
                    var childElementData = elementDataList.Where(cn => cn.ParentID == rootNode.EDID);
                    foreach (var childElement in childElementData)
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = childElement.XmlPath.Substring(childElement.XmlPath.LastIndexOf("\\") + 1);
                        childNode.Name = childElement.EDID;
                        childNode.Tag = childElement.ElementMergeRecordList;
                        root.Nodes.Add(childNode);
                        //childElement.XmlPath;
                        //childElement.EDID;
                        LoopTreeViewChild(childNode, elementDataList);
                    }

                }

            }

        }

        /// <summary>
        /// 循环获取子级信息
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="elementDatas"></param>
        private void LoopTreeViewChild(TreeNode parentNode, List<ElementDataModel> elementDatas)
        {
            var childElementData = elementDatas.Where(cn => cn.ParentID == parentNode.Name);
            foreach (var childElement in childElementData)
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = childElement.XmlPath.Substring(childElement.XmlPath.LastIndexOf("\\") + 1);
                childNode.Name = childElement.EDID;
                childNode.Tag = childElement.ElementMergeRecordList;
                parentNode.Nodes.Add(childNode);
                //循环调用子级
                LoopTreeViewChild(childNode, elementDatas);
            }

        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormShowCaseData_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadTreeView(caseModelDatas);
        }
        /// <summary>
        /// 节点点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeViewCaseStruct_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node != null)
            {
                var mergeList = (e.Node.Tag as List<ElementMergeRecord>);
                dataGridViewSource.DataSource = mergeList;
                if (mergeList != null && mergeList.Count > 0)
                {

                    this.dataGridViewSource.CurrentCell = this.dataGridViewSource[0, 0];
                    dataGridViewSource_CellContentClick(this.dataGridViewSource, new DataGridViewCellEventArgs(0, 0));
                }
            }

        }
        /// <summary>
        /// 点击节点显示历史
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewSource_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string emrid = dataGridViewSource.Rows[e.RowIndex].Cells["EMRID"].Value.ToString();
                var modelist = caseModelDatas[0].ElementDataModelList.Where(a => a.ElementMergeRecordList.FirstOrDefault(b => b.EMRID == emrid) != null);
                if (modelist != null)
                {
                    var motemp = modelist.ToList();
                    dataGridViewHistory.DataSource = motemp[0].ElementMergeRecordList[0].HistoryDataModelList;
                }
            }
        }

    }
     
}
